 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

public class demo1 {
    public int temp;
    public int sum;
    public int kthSmallest(TreeNode root, int k) {
        //进行中序遍历 注意这个是二叉搜索树且关注中序遍历的规律
        sum=k;
        dfs(root);
        return temp;
    }
    public void dfs(TreeNode root){
        if(root==null||sum==0)return;
        dfs(root.left);
        sum--;
        if(sum==0)
            temp=root.val;
        dfs(root.right);
    }
}
